package com.wuye.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;

import com.wuye.common.dao.hibernate.BaseDaoHibernate;
import com.wuye.common.vo.ZTreeInfo;
import com.wuye.dao.AreaDao;
import com.wuye.entity.Area;
import com.wuye.entity.Organization;
import com.wuye.mapper.CommonMapper;
import com.wuye.util.MyBatisSessionFactory;

@Repository("areaDao")
public class AreaDaoImpl extends BaseDaoHibernate implements AreaDao
{
	@SuppressWarnings("unchecked")
	public List<Area> getAreas(int upAreaId) {
		String hql = "select a.* from area a where a.up_area_id = ?";
		@SuppressWarnings("rawtypes")
		List params = new ArrayList();
		params.add(upAreaId);
		return this.jdbcFindList(hql, Area.class, params);
	}

	public List<ZTreeInfo> getAreaInfos(List<Integer> openAreaIds) 
	{
		List<ZTreeInfo> zTreeInfos = null;
		SqlSession session = null;
		
		try 
		{
			session = MyBatisSessionFactory.getSession();			
			CommonMapper commonMapper = session.getMapper(CommonMapper.class);
			zTreeInfos = commonMapper.getAreaInfo(openAreaIds);
		}
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		finally
		{
			session.close();
		}
		
		return zTreeInfos;
	}

	public void addArea(Area area) throws Exception
	{
		super.addObject(area);		
	}

	public void updateArea(Area area) throws Exception 
	{
		super.updateObject(area);
	}

	public void delArea(int areaId) throws Exception
	{
		super.removeObject(Area.class, areaId);
		
	}

	public Area getArea(int areaId) throws Exception 
	{
		return (Area)super.getObject(Area.class, areaId);
	}

	public String getParents(int id)
	{
		SqlSession session = null;
		String parents = null;
		
		try 
		{
			session = MyBatisSessionFactory.getSession();			
			CommonMapper commonMapper = session.getMapper(CommonMapper.class);
			parents = commonMapper.getParents(id);
		}
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		finally
		{
			session.close();
		}
		
		return parents;
	}

}
